<?php

// 本类由系统自动生成，仅供测试用途
class CeshiAction extends BaseAction {
 	public function index()
 	{
		 	$ip = get_client_ip();
		 	var_dump($ip);
		 	$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.$ip);
			$jsonMatches = array();
			preg_match('#\{.+?\}#', $res, $jsonMatches);

			$json = json_decode($jsonMatches[0], true);
			// var_dump($json['city']);
			$cityinfo = M('City')->where('city = "'.$json['city'].'"')->find();
			$city = $cityinfo['pinyin'];
			// header("Location:/".$city.'/');
			// exit;
 		var_dump($city);
 	}

function getIP()
{
       $unknown = 'unknown';
      if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
         $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
         $ip = $_SERVER['REMOTE_ADDR'];
      }
      /**
       * 处理多层代理的情况
       * 或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
       */
      if (false !== strpos($ip, ',')) $ip = reset(explode(',', $ip));
      return $ip;
}
     
}
?>
